<!---
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  NotifyManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--->
<cfcomponent output="false">

	<cffunction name="init" access="public" returntype="EventManager" output="false" hint="Constructor">
		
		<cfset variables.eventListener = structNew() />
		
		<cfset configureEvent() />
		
		<cfreturn this />
	</cffunction>
	
	<cffunction name="setListeners" access="public" returntype="void" hint="Register an event listener to event manager.">
		<cfargument name="listeners" type="array" required="false" default="#arrayNew(1)#" />
		<cfscript>
		var listenerIterator = arguments.listeners.iterator();
		var listener = [];
		var event = "";
		
		while( listenerIterator.hasNext() ){
		
			listener = listenerIterator.next();
			
			event = listener.name;
			
			for( i=1; i<= listLen( event, chr(44) ); i++ ){
			
				registerListener( listGetAt( event, i, chr(44) ), listener.object , listener.async );
			
			}
				
		}
		
		</cfscript>
	</cffunction>
	
	<cffunction name="registerListener" access="public" returntype="void">
		<cfargument name="name" type="String" required="true" />
		<cfargument name="object" type="Any" required="true" />
		<cfargument name="async" type="boolean" required="true" />
		<cfset var listener = structNew() />
		
		<cfif not eventExists( arguments.name )>
			<cfabort showerror="Missing #arguments.name# event ! Please add an event to cfc" />
		</cfif>
		
		<cfset listener[ 'object' ] = arguments.object />
		<cfset listener[ 'async' ] = arguments.async />
		
		<cfset variables.eventListener[ arguments.name ].listeners.add( listener ) />
		
	</cffunction>
	
	<cffunction name="unregisterListener" access="public" returntype="void">
		<cfargument name="name" type="String" required="true" />
		<cfargument name="object" type="Any" required="true" />
		
		<cfset variables.eventListener[ arguments.name ].remove( arguments.object ) />
	</cffunction>

	<cffunction name="eventExists" access="public" returntype="boolean">
		<cfargument name="name" type="String" required="true" />
		<cfreturn structKeyExists( variables.eventListener, arguments.name ) />
	</cffunction>

	<!--- Utils --->
	
	<cffunction name="clearCounters" access="public" returntype="void">
		<cfset var i = "" />
		
		<cfloop collection="#variables.eventListener#" item="i">
			<cfset variables.eventListener[ i ].statistics = 0 />
		</cfloop>
		
	</cffunction>
	
	<!--- Private --->
	
	<cffunction name="configureEvent" access="private" returntype="void" output="false">
		<cfabort showerror="Abstract !">
	</cffunction>
	
	<cffunction name="addEvent" access="private" returntype="void">
		<cfargument name="name" type="String" required="true" hint="Event name" />
		<cfscript>
		if( not eventExists( arguments.name ) ){
		
			variables.eventListener[ arguments.name ] = structNew();
			variables.eventListener[ arguments.name ].listeners = arrayNew(1);
			variables.eventListener[ arguments.name ].statistics = 0;
		
		}
		</cfscript>
	</cffunction>
	
	<cffunction name="getCounter" access="private" returntype="Numeric">
		<cfargument name="name" type="String" required="true" />
		<cfreturn variables.eventListener[ arguments.name ].statistics />
	</cffunction>
	
	<cffunction name="incr" access="private" returntype="void" hint="Increment stats">
		<cfargument name="name" type="String" required="true" />
		<cfset var s = variables.eventListener[ arguments.name ].statistics />
		<cfset s = s + 1 />
	</cffunction>
	
	<cffunction name="fireEvent" access="private" returntype="void">
		<cfargument name="name" type="String" required="true" hint="Event name" />
		<cfargument name="args" type="Struct" required="false" default="#structNew()#" hint="Event name" />
		<cfset var listeners = variables.eventListener[arguments.name].listeners.iterator() />
		<cfset var listener = structNew() />
		
		<cfloop condition="#listeners.hasNext()#">
		
			<cfset listener = listeners.next() />
			
			<cfif listener.async >
			
				<cfthread name="#arguments.name#_#createUUID()#" action="run" component="#listener.object#" method="#arguments.name#" argumentcollection="#arguments.args#" >
					
					<cfinvoke component="#component#" method="#method#" argumentcollection="#argumentcollection#" />
			
				</cfthread>
				
			<cfelse>
			
				<cfinvoke component="#listener.object#" method="#arguments.name#" argumentcollection="#arguments.args#" />
			
			</cfif>
			
		</cfloop>
		
		<cfset incr( arguments.name ) />
		
	</cffunction>

</cfcomponent>